package cn.stronglink.assetmanage.business.inspection.view;

import android.app.Dialog;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.View;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.check.view.CheckFragment;
import cn.stronglink.assetmanage.business.inspection.adapter.PatrolPagerAdapter;
import cn.stronglink.assetmanage.utils.ToastUtil;
import cn.stronglink.assetmanage.widget.CustomProgressDialog;

/**
 * 巡检查询主fragment
 * Created by zhengjinglan on 2018/6/13.
 */

public class PatrolView {

    private Fragment fragment;
    @BindView(R.id.inspection_tabLayout)
    TabLayout inspection_tabLayout;
    @BindView(R.id.inspection_view_pager)
    ViewPager repair_view_pager;
    private String[] mTitle = {
            MyApplication.APP_CONTEXT.getResources().getString(R.string.not_repair),
            MyApplication.APP_CONTEXT.getResources().getString(R.string.already_repair),
            MyApplication.APP_CONTEXT.getResources().getString(R.string.repair_register)
    };
    private PatrolPagerAdapter patrolPagerAdapter;
    private FragmentManager fm;
    Dialog mDialog;
    public PatrolView(Fragment fragment, View view){
        this.fragment = fragment;
        ButterKnife.bind(this, view);
        initView();
    }

    private void initView(){
        fm = fragment.getChildFragmentManager();
        patrolPagerAdapter = new PatrolPagerAdapter(fm, mTitle);
        repair_view_pager.setAdapter(patrolPagerAdapter);
        inspection_tabLayout.setupWithViewPager(repair_view_pager);
        inspection_tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                repair_view_pager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    public void showProgressDialog(String msg){
        if(mDialog!=null){
            mDialog.cancel();
            mDialog=null;
        }
        mDialog = CustomProgressDialog.createLoadingDialog(fragment.getActivity(), msg);
        mDialog.setCancelable(false);
        mDialog.show();
    }

    public void hideProgressDialog(){
        if(mDialog!=null){
            mDialog.cancel();
        }
    }

    public void showToast(String msg){
        ToastUtil.toastShort(msg);
    }

    public void setCurrentFragment(int index) {
        repair_view_pager.setCurrentItem(index);
    }

    public void refreshView() {
        //重新显示PatrolFragment
        Fragment patrolFragment = new PatrolFragment();
        FragmentManager fragmentManager = fragment.getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frame_business, patrolFragment);
        transaction.commit();
    }
}
